/**
 * 
 */
package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IHomeSiteResearchReportFileUploadDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.form.researchReport.HomeSiteResearchReportFileUploadForm;
import com.wsj.web.vo.homeSite.HomeSiteUploadFile;

/**
 * @author fengbin  createTime:2009-7-25
 *
 */
public class HomeSiteResearchReportFileUploadDaoImpl extends HibernateDaoSupport implements
		IHomeSiteResearchReportFileUploadDao {

	
	/******
	 * 添加研究报告
	 */
	public void addHomeSiteUploadFile(HomeSiteUploadFile homeSiteUploadFile) throws DataAccessException {
		this.getHibernateTemplate().save(homeSiteUploadFile);
	}

	/******
	 * 通过id,获取研究报告对象
	 */
	public HomeSiteUploadFile getHomeSiteUploadFileById(Integer id) throws DataAccessException {
		
		return (HomeSiteUploadFile) this.getHibernateTemplate().get(HomeSiteUploadFile.class, id);
	}

	/********
	 * 获取研究报告列表
	 * 
	 * 
	 */
	public PageResultSet searchResearchReportList(HomeSiteResearchReportFileUploadForm homeSiteResearchReportFileUploadForm, int page, int pageSize) throws DataAccessException {
		 
		//初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from HomeSiteUploadFile as uf where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        //文件名
        List<Comparable> list = new ArrayList<Comparable>();
        if(homeSiteResearchReportFileUploadForm.getFileTitle()!= null&&!homeSiteResearchReportFileUploadForm.getKeyWords().equals("")){
            stringBuffer.append(" and uf.fileTitle like ?");
            list.add("%"+homeSiteResearchReportFileUploadForm.getFileTitle()+"%");
        }
        
        //关键字
        if(homeSiteResearchReportFileUploadForm.getKeyWords()!= null&&!homeSiteResearchReportFileUploadForm.getFileTitle().equals("")){
            stringBuffer.append(" and uf.fileDescription like ?");
            list.add("%"+homeSiteResearchReportFileUploadForm.getKeyWords()+"%");
        }
        
        //研究机构
        if(homeSiteResearchReportFileUploadForm.getBmResearchInstitutionId()!= -1 && homeSiteResearchReportFileUploadForm.getBmResearchInstitutionId()!=0){
        	stringBuffer.append(" and uf.bmResearchInstitution.id = ?");
        	list.add(homeSiteResearchReportFileUploadForm.getBmResearchInstitutionId());
        }
        
        //文件类型
        if(homeSiteResearchReportFileUploadForm.getBmFileTypeId()!=-1  && homeSiteResearchReportFileUploadForm.getBmFileTypeId()!=0){
        	stringBuffer.append(" and uf.bmFileType.id = ?");
        	list.add(homeSiteResearchReportFileUploadForm.getBmFileTypeId());
        }
        
        //日期查询
        if(homeSiteResearchReportFileUploadForm.getGmtCreateStr()!= null
        		&&!homeSiteResearchReportFileUploadForm.getGmtCreateStr().equals("")
        		&&homeSiteResearchReportFileUploadForm.getGmtCreateEndStr()!=null
        		&&homeSiteResearchReportFileUploadForm.getGmtCreateEndStr().equals("")){
        	
    			stringBuffer.append(" and uf.gmtCreate >= '"+homeSiteResearchReportFileUploadForm.getGmtCreateStr().trim() +" 00:00:00'"+" and uf.gmtModified< '"+homeSiteResearchReportFileUploadForm.getGmtCreateEndStr().trim()+" 23:59:59'");
        	
        }
        
        
        //文件类型.是否是加精的研究报告
        if(homeSiteResearchReportFileUploadForm.getReportType()!=null
        		&& !homeSiteResearchReportFileUploadForm.getReportType().equals("")){
        	stringBuffer.append("  and uf.reportType = ?");
        	list.add(homeSiteResearchReportFileUploadForm.getReportType());
        }
        
        //状态
        if(homeSiteResearchReportFileUploadForm.getStatus()!=null){
        	
        	stringBuffer.append(" and uf.status = ?");
        	list.add(homeSiteResearchReportFileUploadForm.getStatus());
        	
        }else{
        	stringBuffer.append(" and uf.status = 0");	
        }
        
        
		stringBuffer.append(" order by uf.gmtCreate desc ");
		
		 String[] strParams = Tool.toArray(list);
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session, strParams);

		return pageResultSet;
		
	}

	/********
	 * 更新研究报告对象
	 */
	public void updateHomeSiteUploadFile(HomeSiteUploadFile dbHomeSiteUploadFile) throws DataAccessException {
		
		this.getHibernateTemplate().update(dbHomeSiteUploadFile);
		
	}

	
	/***********
	 * 获取前台的研究报告
	 */
	public PageResultSet getAllResearchReportList(int page, int pageSize) throws DataAccessException {
//		初始化
		PageResultSet pageResultSet = new PageResultSet();

		PerfectPageResultSet pageResult = new PerfectPageResultSet();

		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),
				true);

		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer("from HomeSiteUploadFile as uf where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        //状态
        stringBuffer.append(" and uf.status = 0");
        
		stringBuffer.append(" order by uf.gmtCreate desc ");
		
	        pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),
	                session);

		return pageResultSet;
		
	}

	/*******
	 * 获取前几个研究报告
	 */
	public List<HomeSiteUploadFile> getResearchReportTop(int top) throws DataAccessException {
		
		String sql=" from  HomeSiteUploadFile h  order by h.gmtCreate desc  ";
		Query q=this.getSession().createQuery(sql);
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
	}

	


}
